package com.zzj.hotel.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.zzj.hotel.core.entity.Classify;
import com.zzj.hotel.core.entity.Picture;
import com.zzj.hotel.core.entity.Room;
import com.zzj.hotel.core.entity.User;
import com.zzj.hotel.core.utils.AjaxUtil;
import com.zzj.hotel.core.utils.FileUtil;
import com.zzj.hotel.core.utils.IdManager;
import com.zzj.hotel.service.ClassifyService;
import com.zzj.hotel.service.PictureService;
import com.zzj.hotel.service.RoomService;
import com.zzj.hotel.service.UserService;

@Controller
@RequestMapping(value={"/classify"})
public class ClassifyController {
	@Autowired ClassifyService classifyService;
	@Autowired PictureService pictureService;
	@RequestMapping(value={"/orign"}, method=RequestMethod.POST)
	public void orign(HttpServletRequest request, HttpServletResponse response){
		List<Classify> list = classifyService.findList("zzj");
		Map<String,Object> map = new HashMap<String,Object>();
		for(Classify classify:list){
			map.put(classify.getClassify_id(), classify);
		}
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}

	@RequestMapping(value={"/getparent"}, method=RequestMethod.POST)
	public void getparent(HttpServletRequest request, HttpServletResponse response){
		String classifyId = request.getParameter("classifyId");
		Classify parent = null;
		if(classifyId != null && !"".equals(classifyId.trim())){
			Classify children = classifyService.findById(classifyId);
			if(children != null){
				parent = classifyService.findById(children.getClassify_belongto());
			}
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("p_classifyId", parent.getClassify_id());
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}
	@RequestMapping(value={"/children"}, method=RequestMethod.POST)
	public void children(HttpServletRequest request, HttpServletResponse response){
		String classifyId = request.getParameter("classifyId");
		List<Classify> list = classifyService.findList(classifyId);
		Map<String,Object> map = new HashMap<String,Object>();
		for(Classify classify:list){
			map.put(classify.getClassify_id(), classify);
		}
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}

	@RequestMapping(value={"/update"}, method=RequestMethod.POST)
	public void update(HttpServletRequest request, HttpServletResponse response){
		String classifyId = request.getParameter("classifyId");
		String classify_pid = request.getParameter("classify_pid");
		String classifyName = request.getParameter("classifyName");
		Classify classify1 = classifyService.findById(classifyId);
		if(classify1 != null){
			classify1.setClassify_name(classifyName);
		}
		classifyService.updateClassify(classify1);
		List<Classify> list = classifyService.findList(classify_pid);
		Map<String,Object> map = new HashMap<String,Object>();
		for(Classify classify:list){
			map.put(classify.getClassify_id(), classify);
		}
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}

	@RequestMapping(value={"/delete"}, method=RequestMethod.POST)
	public void delete(HttpServletRequest request, HttpServletResponse response){
		String classifyId = request.getParameter("classifyId");
		String classify_pid = request.getParameter("classify_pid");
		classifyService.deleteClassify(classifyId);
		List<Classify> list = classifyService.findList(classify_pid);
		Map<String,Object> map = new HashMap<String,Object>();
		for(Classify classify:list){
			map.put(classify.getClassify_id(), classify);
		}
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}

	@RequestMapping(value={"/add"}, method=RequestMethod.POST)
	public void add(HttpServletRequest request, HttpServletResponse response){
		String id = IdManager.getId();
		String classify_pid = request.getParameter("classify_pid");
		String classify_name = request.getParameter("new_calssifyName");
		String imgPath = request.getParameter("image");
		String filename = FileUtil.copyFile(imgPath, id,request);
		String author = request.getSession().getAttribute("userName").toString();
		String authorId = request.getSession().getAttribute("userId").toString();
		Classify classify1 = new Classify();
		classify1.setClassify_id(id);
		classify1.setClassify_name(classify_name);
		classify1.setClassify_belongto(classify_pid);
		classify1.setClassify_author(author);
		classify1.setClassify_authorId(authorId);
		classify1.setClassify_createTime(new SimpleDateFormat("yyyy-MM-dd HH;mm;ss").format(new Date()));
		classify1.setClassify_picture(filename);
		classifyService.insertClassify(classify1);
		//		Picture picture = new Picture();
		//		picture.setPicture_id(IdManager.getId());
		//		picture.setPicture_classifyId(id);
		//		picture.setPicture_name(filename);
		//		picture.setPicture_author(author);
		//		picture.setPicture_authorId(authorId);
		//		picture.setPicture_time(new SimpleDateFormat("yyyy-MM-dd HH;mm;ss").format(new Date()));
		//		pictureService.addPicture(picture);
		List<Classify> list = classifyService.findList(classify_pid);
		Map<String,Object> map = new HashMap<String,Object>();
		for(Classify classify:list){
			map.put(classify.getClassify_id(), classify);
		}
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}

}
